<!--
- Author:   .
- Date:    2018/1/11 0011.
- File:    search.
-->
<template>
  <div>
    <div class="searchpage f-cb">
      <div class="SearchBox2">
        <div class="SearchBor xd hidden"><i class="iconfont icon-search lh70"></i>
          <input type="text" class="SearchInp pl80" placeholder="输入用户名或电话搜索" v-model="keyword" v-on:keyup.13="search">
          <button type="button" class="SearchBut" @click="search">搜索</button>
        </div>
      </div>
      <div class="searchclose" @click="cancleSearch">取消</div>
    </div>
    <div v-infinite-scroll="loadMore"
      infinite-scroll-disabled="false"
      infinite-scroll-distance="0"
      infinite-scroll-immediate-check="false">
      <div class="inter_tab" v-for="history in historyList.data"><a>
        <table width="100%" border="0" @click="gotoOrderDetail(history.order_id, 'true')">
          <tr>
            <td>类型：{{history.type | userLvTypeName}}</td>
            <td>下单时间：{{history.add_time | timestampToDate('yyyy-MM-dd')}}</td>
          </tr>
          <tr>
            <td width="50%">贡献红积分：<span class="cr_f83166">￥{{history.freeze_balance}}</span></td>
            <td v-show="history.type == $dictionary.userLv.a">合计：<span class="cr_f83166">￥{{parseFloat(history.order_amount) + parseFloat(history.freeze_balance)}} </span>
            </td>
          </tr>
          <tr>
            <td width="50%">本金：<span>￥{{history.order_amount}}</span></td>
            <td>购买人：{{history.type == $dictionary.userLv.a ? '自己' : history.member_truename}}</td>
          </tr>
        </table>
      </a></div>
    </div>
    <loading-more :allLoaded="historyList.allLoaded" :show="historyList.data.length > 0"></loading-more>
    <empty-data :show="historyList.allLoaded && historyList.data.length <= 0"></empty-data>
    <footer-tab></footer-tab>
  </div>
</template>

<script>
  import {mapModules, mapRules} from 'vuet'

  export default{
    mixins: [
      mapModules({memberInfo: 'memberInfo'}),
      mapRules({need: ['memberInfo']})
    ],
    data() {
      return {
        keyword: '',
        historyList: {requested: false, loading: false, allLoaded: false, page: 1, pageSize: 10, data: []},
        currentDate: new Date(),
        selectMonth: '',
        selectUserLv: ''
      }
    },
    created() {
    },
    computed: {},
    watch: {
      selectUserLv: function (value) {
        this.selectUserLv = value
      }
    },
    methods: {
      cancleSearch() {
        this.keyword = ''
        this.historyList = {requested: false, loading: false, allLoaded: false, page: 1, pageSize: 10, data: []}
        this.$router.go(-1)
      },
      search() {
        if (!this.keyword) {
          this.$toast('请输入用户名或电话搜索')
          return
        }
        this.initBalanceHistory()
      },
      gotoOrderDetail(orderId, isSelf) {
        this.$router.push({name: 'order-detail', params: {id: orderId}, query: {self: isSelf}})
      },
      openDatePicker() {
        this.$refs.datePicker.open()
      },

      handleDateChange(value) {
        let date = new Date(value)
        let dateStr = this.$utils.formatDate(date, 'yyyy-MM-dd')
        this.selectMonth = dateStr
        console.log(dateStr)
        this.initBalanceHistory()
      },
      initBalanceHistory() {
        this.historyList = {requested: false, loading: false, allLoaded: false, page: 1, pageSize: 10, data: []}
        this.getIntegralHistory(this.historyList.page, this.historyList.pageSize)
      },
      getIntegralHistory(page, pageSize) {
        if (!this.keyword || this.keyword == '') {
          return
        }
        this.historyList.loading = true
        let params = {
          keyword: this.keyword,
          page: page,
          pageSize: pageSize
        }
        this.$request.getHongIntegralIncome(params).then(data => {
          if (data.length < this.historyList.pageSize) {
            this.historyList.allLoaded = true
          }
          this.historyList.data = [...this.historyList.data, ...data]
        }).finally(() => {
          this.historyList.requested = true
          this.historyList.loading = false
        })
      },
      loadMore() {
        if (this.historyList.loading || this.historyList.allLoaded) { // 正在加载 或 已取得全部数据
          return
        }
        setTimeout(() => {
          this.getIntegralHistory(++this.historyList.page, this.historyList.pageSize)
        }, 500)
      }
    }
  }
</script>

<style>

</style>